<!DOCTYPE html>
<html lang=en>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>使用ResNet34和MobileNetV2学习CIFAR10数据集 | 鲨鱼之家</title>
  <meta name="description" content="附0：将.ipynb文件转换为markdown的方法 前言  环境 开发框架 具体情况   准备工作  使用Google Colab并且改变当前系统默认目录 导入库 使用GPU 参数配置   加载数据集 定义模型 定义训练、验证和测试方法 保存模型参数 对输入的图片进行预测 MobileNetV2部分（仅包含模型）   附0：将.ipynb文件转换为markdown的方法   通过命令终端，进">
<meta property="og:type" content="article">
<meta property="og:title" content="使用ResNet34和MobileNetV2学习CIFAR10数据集">
<meta property="og:url" content="http://tina-yao.gitee.io/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/index.html">
<meta property="og:site_name" content="大鲨鱼">
<meta property="og:description" content="附0：将.ipynb文件转换为markdown的方法 前言  环境 开发框架 具体情况   准备工作  使用Google Colab并且改变当前系统默认目录 导入库 使用GPU 参数配置   加载数据集 定义模型 定义训练、验证和测试方法 保存模型参数 对输入的图片进行预测 MobileNetV2部分（仅包含模型）   附0：将.ipynb文件转换为markdown的方法   通过命令终端，进">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/ResNet/1.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/ResNet/2.png">
<meta property="article:published_time" content="2022-08-06T11:28:16.000Z">
<meta property="article:modified_time" content="2022-08-07T22:27:40.571Z">
<meta property="article:author" content="BigbigShark">
<meta property="article:tag" content="代码">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/ResNet/1.png">
  <!-- Canonical links -->
  <link rel="canonical" href="http://tina-yao.gitee.io/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/index.html">
  
    <link rel="alternate" href="/atom.xml" title="大鲨鱼" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/bigbig-shark/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body class="main-center theme-purple# 主题颜色 theme-black theme-blue theme-green theme-purple" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank">
          <img class="img-circle img-rotate" src="/bigbig-shark/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">大鲨鱼</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">CV&amp;Robots</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Wuhan, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="Type something..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/bigbig-shark/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">Home</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/bigbig-shark/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">Archives</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/bigbig-shark/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">Categories</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/bigbig-shark/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">Tags</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/bigbig-shark/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">Links</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/bigbig-shark/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">About</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">Categories</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/">AI</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/ML/">ML</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Concepts/">Concepts</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Robots/">Robots</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E5%8D%8A%E6%97%A5%E9%97%B2/">半日闲</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1/">数学建模</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/">数据分析</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/">计算机视觉</a><span class="category-list-count">30</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget-body tagcloud">
      <a href="/bigbig-shark/tags/CNN-backbones/" style="font-size: 13.67px;">CNN_backbones</a> <a href="/bigbig-shark/tags/OpenCV/" style="font-size: 13px;">OpenCV</a> <a href="/bigbig-shark/tags/Python/" style="font-size: 13px;">Python</a> <a href="/bigbig-shark/tags/ROS/" style="font-size: 13px;">ROS</a> <a href="/bigbig-shark/tags/%E4%BB%A3%E7%A0%81/" style="font-size: 13px;">代码</a> <a href="/bigbig-shark/tags/%E5%85%83%E5%AE%87%E5%AE%99/" style="font-size: 13px;">元宇宙</a> <a href="/bigbig-shark/tags/%E5%85%B4%E8%B6%A3/" style="font-size: 13px;">兴趣</a> <a href="/bigbig-shark/tags/%E5%9C%BA%E6%99%AF%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB/" style="font-size: 13px;">场景文字识别</a> <a href="/bigbig-shark/tags/%E6%91%98%E6%8A%84/" style="font-size: 13.33px;">摘抄</a> <a href="/bigbig-shark/tags/%E6%95%B0%E6%8D%AE%E9%9B%86/" style="font-size: 13px;">数据集</a> <a href="/bigbig-shark/tags/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/" style="font-size: 14px;">目标检测</a> <a href="/bigbig-shark/tags/%E7%BE%8E%E8%B5%9B/" style="font-size: 13px;">美赛</a> <a href="/bigbig-shark/tags/%E8%81%9A%E7%B1%BB/" style="font-size: 13px;">聚类</a> <a href="/bigbig-shark/tags/%E8%87%AA%E5%8A%A8%E9%A9%BE%E9%A9%B6/" style="font-size: 13.33px;">自动驾驶</a> <a href="/bigbig-shark/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%893D/" style="font-size: 13px;">计算机视觉3D</a> <a href="/bigbig-shark/tags/%E8%BD%BB%E9%87%8F%E7%BA%A7/" style="font-size: 13px;">轻量级</a>
    </div>
  </div>

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-使用ResNet34和MobileNetV2学习CIFAR10数据集" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      使用ResNet34和MobileNetV2学习CIFAR10数据集
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/" class="article-date">
	  <time datetime="2022-08-06T11:28:16.000Z" itemprop="datePublished">2022-08-06</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/bigbig-shark/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/">计算机视觉</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/bigbig-shark/tags/%E4%BB%A3%E7%A0%81/" rel="tag">代码</a>
  </span>


        

	<span class="article-read hidden-xs">
    	<i class="icon icon-eye-fill" aria-hidden="true"></i>
    	<span id="/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/" class="leancloud_visitors"  data-flag-title="使用ResNet34和MobileNetV2学习CIFAR10数据集">
			<span class="leancloud-visitors-count">0</span>
		</span>
    </span>

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/#comments" class="article-comment-link">Comments</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">Word Count: 1.9k(words)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">Read Count: 10(minutes)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <!-- toc -->
<ul>
<li><a href="#%E9%99%840%E5%B0%86ipynb%E6%96%87%E4%BB%B6%E8%BD%AC%E6%8D%A2%E4%B8%BAmarkdown%E7%9A%84%E6%96%B9%E6%B3%95">附0：将.ipynb文件转换为markdown的方法</a></li>
<li><a href="#%E5%89%8D%E8%A8%80">前言</a>
<ul>
<li><a href="#%E7%8E%AF%E5%A2%83">环境</a></li>
<li><a href="#%E5%BC%80%E5%8F%91%E6%A1%86%E6%9E%B6">开发框架</a></li>
<li><a href="#%E5%85%B7%E4%BD%93%E6%83%85%E5%86%B5">具体情况</a></li>
</ul>
</li>
<li><a href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C">准备工作</a>
<ul>
<li><a href="#%E4%BD%BF%E7%94%A8google-colab%E5%B9%B6%E4%B8%94%E6%94%B9%E5%8F%98%E5%BD%93%E5%89%8D%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%9B%AE%E5%BD%95">使用Google Colab并且改变当前系统默认目录</a></li>
<li><a href="#%E5%AF%BC%E5%85%A5%E5%BA%93">导入库</a></li>
<li><a href="#%E4%BD%BF%E7%94%A8gpu">使用GPU</a></li>
<li><a href="#%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE">参数配置</a></li>
</ul>
</li>
<li><a href="#%E5%8A%A0%E8%BD%BD%E6%95%B0%E6%8D%AE%E9%9B%86">加载数据集</a></li>
<li><a href="#%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9E%8B">定义模型</a></li>
<li><a href="#%E5%AE%9A%E4%B9%89%E8%AE%AD%E7%BB%83-%E9%AA%8C%E8%AF%81%E5%92%8C%E6%B5%8B%E8%AF%95%E6%96%B9%E6%B3%95">定义训练、验证和测试方法</a></li>
<li><a href="#%E4%BF%9D%E5%AD%98%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0">保存模型参数</a></li>
<li><a href="#%E5%AF%B9%E8%BE%93%E5%85%A5%E7%9A%84%E5%9B%BE%E7%89%87%E8%BF%9B%E8%A1%8C%E9%A2%84%E6%B5%8B">对输入的图片进行预测</a></li>
<li><a href="#mobilenetv2%E9%83%A8%E5%88%86%E4%BB%85%E5%8C%85%E5%90%AB%E6%A8%A1%E5%9E%8B">MobileNetV2部分（仅包含模型）</a></li>
</ul>
<!-- tocstop -->
<h2><span id="附0将ipynb文件转换为markdown的方法">附0：将.ipynb文件转换为markdown的方法</span></h2>
<ol>
<li>
<p>通过命令终端，进入.ipynb文件所在目录；</p>
</li>
<li>
<p>输入以下内容：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jupyter nbconvert --to markdown ResNet.ipynb <span class="comment"># 转换名为&quot;ResNet.ipynb&quot;的.md文件</span></span><br><span class="line"><span class="comment"># 或者同时转换批量文件</span></span><br><span class="line">jupyter nbconvert --to markdown *.ipynb</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2><span id="前言">前言</span></h2>
<h3><span id="环境">环境</span></h3>
<p>Google Colab</p>
<h3><span id="开发框架">开发框架</span></h3>
<p>PyTorch</p>
<h3><span id="具体情况">具体情况</span></h3>
<p>搭建了ResNet34模型，并在CIFAR10数据集上进行训练和测试。<strong>最终测试集准确率为83.55%</strong>；符合该模型的能力。</p>
<h2><span id="准备工作">准备工作</span></h2>
<h3><span id="使用google-colab并且改变当前系统默认目录">使用Google Colab并且改变当前系统默认目录</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># This mounts your Google Drive to the Colab VM.</span></span><br><span class="line"><span class="keyword">from</span> google.colab <span class="keyword">import</span> drive</span><br><span class="line">drive.mount(<span class="string">&#x27;/content/drive&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># Enter the foldername in your Drive where you have saved the unzipped</span></span><br><span class="line">FOLDERNAME = <span class="string">&#x27;CIFAR10_colab/&#x27;</span></span><br><span class="line"><span class="keyword">assert</span> FOLDERNAME <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>, <span class="string">&quot;[!] Enter the foldername.&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Now that we&#x27;ve mounted your Drive, this ensures that</span></span><br><span class="line"><span class="comment"># the Python interpreter of the Colab VM can load</span></span><br><span class="line"><span class="comment"># python files from within it.</span></span><br><span class="line"><span class="comment"># Add search path</span></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">sys.path.append(<span class="string">&#x27;/content/drive/My Drive/&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(FOLDERNAME))</span><br><span class="line"></span><br><span class="line"><span class="comment"># This downloads the CIFAR-10 dataset to your Drive</span></span><br><span class="line"><span class="comment"># if it doesn&#x27;t already exist.</span></span><br><span class="line">%cd /content/drive/My\ Drive/$FOLDERNAME/cifar-<span class="number">10</span>-py/</span><br><span class="line">!bash get_datasets.sh</span><br><span class="line">%cd /content/drive/My\ Drive/$FOLDERNAME</span><br></pre></td></tr></table></figure>
<pre><code>Mounted at /content/drive
/content/drive/My Drive/CIFAR10_colab/cifar-10-py
bash: get_datasets.sh: No such file or directory
/content/drive/My Drive/CIFAR10_colab
</code></pre>
<h3><span id="导入库">导入库</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> sampler</span><br><span class="line"><span class="keyword">import</span> torchvision.datasets <span class="keyword">as</span> dst</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> T</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br></pre></td></tr></table></figure>
<h3><span id="使用gpu">使用GPU</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">USE_GPU = <span class="literal">True</span></span><br><span class="line">dtype = torch.float32</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> USE_GPU <span class="keyword">and</span> torch.cuda.is_available():</span><br><span class="line">  device = torch.device(<span class="string">&#x27;cuda&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">  device = torch.device(<span class="string">&#x27;cpu&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;using device:&quot;</span>, device)</span><br></pre></td></tr></table></figure>
<pre><code>using device: cuda
</code></pre>
<h3><span id="参数配置">参数配置</span></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">config = &#123;</span><br><span class="line">    <span class="string">&#x27;batch_size&#x27;</span>: <span class="number">64</span>,</span><br><span class="line">    <span class="string">&#x27;print_every&#x27;</span>: <span class="number">10</span>, <span class="comment"># 控制打印损失值的频率</span></span><br><span class="line">    <span class="string">&#x27;NUM_TRAIN&#x27;</span>: <span class="number">49000</span>, <span class="comment"># 用于训练的样本数，则剩下的用来做验证样本</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2><span id="加载数据集">加载数据集</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">myDataLoader</span>():</span></span><br><span class="line">  batch_size = config[<span class="string">&#x27;batch_size&#x27;</span>]</span><br><span class="line">  NUM_TRAIN = config[<span class="string">&#x27;NUM_TRAIN&#x27;</span>]</span><br><span class="line"></span><br><span class="line">  <span class="comment"># transform是datasets中的一个图像预处理</span></span><br><span class="line">  transform = T.Compose([</span><br><span class="line">      T.Resize((<span class="number">32</span>, <span class="number">32</span>)), <span class="comment"># CIFAR10本身就是32x32的RGB图</span></span><br><span class="line">      T.ToTensor(), <span class="comment"># 转换为张量</span></span><br><span class="line">      T.Normalize((<span class="number">0.4914</span>, <span class="number">0.4822</span>, <span class="number">0.4465</span>), (<span class="number">0.2023</span>, <span class="number">0.1994</span>, <span class="number">0.2010</span>))</span><br><span class="line">      <span class="comment"># 关于此处归一化的解释：https://blog.csdn.net/qq_38765642/article/details/109779370</span></span><br><span class="line">  ])</span><br><span class="line"></span><br><span class="line">  train_set = dst.CIFAR10(root=<span class="string">&#x27;cifar-10-py/&#x27;</span>, train=<span class="literal">True</span>, transform=transform, download=<span class="literal">True</span>)</span><br><span class="line">  train_loader = DataLoader(train_set, batch_size=batch_size, sampler=sampler.SubsetRandomSampler(<span class="built_in">range</span>(NUM_TRAIN)))</span><br><span class="line">  val_loader = DataLoader(train_set, batch_size=batch_size, )</span><br><span class="line"></span><br><span class="line">  test_set = dst.CIFAR10(root=<span class="string">&#x27;cifar-10-py/&#x27;</span>, train=<span class="literal">False</span>, transform=transform, download=<span class="literal">True</span>)</span><br><span class="line">  test_loader = DataLoader(test_set, batch_size=batch_size) <span class="comment"># DataLoader的shuffle默认为False</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> train_loader, val_loader, test_loader</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">train_loader, val_loader, test_loader = myDataLoader()</span><br></pre></td></tr></table></figure>
<pre><code>Files already downloaded and verified
Files already downloaded and verified
</code></pre>
<h2><span id="定义模型">定义模型</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BasicBlock</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  expansion = <span class="number">1</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, in_channels, out_channels, stride=<span class="number">1</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    self.residual_function = nn.Sequential(</span><br><span class="line">        nn.Conv2d(in_channels, out_channels, kernel_size=<span class="number">3</span>, stride=stride, padding=<span class="number">1</span>, bias=<span class="literal">False</span>),</span><br><span class="line">        nn.BatchNorm2d(out_channels),</span><br><span class="line">        nn.ReLU(inplace=<span class="literal">True</span>),</span><br><span class="line">        nn.Conv2d(out_channels, out_channels * BasicBlock.expansion, kernel_size=<span class="number">3</span>, padding=<span class="number">1</span>, bias=<span class="literal">False</span>),</span><br><span class="line">        nn.BatchNorm2d(out_channels * BasicBlock.expansion),</span><br><span class="line">    )</span><br><span class="line">    </span><br><span class="line">    self.shortcut = nn.Sequential()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># use 1x1 conv to match the dimension</span></span><br><span class="line">    <span class="keyword">if</span> stride != <span class="number">1</span> <span class="keyword">or</span> in_channels != out_channels * BasicBlock.expansion:</span><br><span class="line">      self.shortcut = nn.Sequential(</span><br><span class="line">          nn.Conv2d(in_channels, out_channels * BasicBlock.expansion, kernel_size=<span class="number">1</span>, stride=stride, bias=<span class="literal">False</span>),</span><br><span class="line">          nn.BatchNorm2d(out_channels * BasicBlock.expansion),</span><br><span class="line">      )</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, X</span>):</span></span><br><span class="line">    <span class="keyword">return</span> nn.ReLU(inplace=<span class="literal">True</span>)(self.residual_function(X) + self.shortcut(X))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ResNet</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, block, num_block, num_classes=<span class="number">10</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    self.in_channels = <span class="number">64</span></span><br><span class="line"></span><br><span class="line">    self.conv1 = nn.Sequential(</span><br><span class="line">        nn.Conv2d(<span class="number">3</span>, <span class="number">64</span>, kernel_size=<span class="number">3</span>, padding=<span class="number">1</span>, bias=<span class="literal">False</span>),</span><br><span class="line">        nn.BatchNorm2d(<span class="number">64</span>),</span><br><span class="line">        nn.ReLU(inplace=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line">    self.conv2 = self.make_layer(block, <span class="number">64</span>, num_block[<span class="number">0</span>], <span class="number">1</span>) <span class="comment"># 3</span></span><br><span class="line">    self.conv3 = self.make_layer(block, <span class="number">128</span>, num_block[<span class="number">1</span>], <span class="number">2</span>) <span class="comment"># 4</span></span><br><span class="line">    self.conv4 = self.make_layer(block, <span class="number">256</span>, num_block[<span class="number">2</span>], <span class="number">2</span>) <span class="comment"># 6</span></span><br><span class="line">    self.conv5 = self.make_layer(block, <span class="number">512</span>, num_block[<span class="number">3</span>], <span class="number">2</span>) <span class="comment"># 3</span></span><br><span class="line">    self.avg_pool = nn.AdaptiveAvgPool2d((<span class="number">1</span>, <span class="number">1</span>))</span><br><span class="line">    self.fc = nn.Linear(<span class="number">512</span> * BasicBlock.expansion, num_classes)</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">make_layer</span>(<span class="params">self, block, out_channels, num_blocks, stride</span>):</span></span><br><span class="line">    strides = [stride] + [<span class="number">1</span>] * (num_blocks - <span class="number">1</span>)</span><br><span class="line">    layers = []</span><br><span class="line">    <span class="keyword">for</span> stride <span class="keyword">in</span> strides:</span><br><span class="line">      layers.append(block(self.in_channels, out_channels, stride))</span><br><span class="line">      self.in_channels = out_channels * block.expansion</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> nn.Sequential(*layers)</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, X</span>):</span></span><br><span class="line">    output = self.conv1(X)</span><br><span class="line">    output = self.conv2(output)</span><br><span class="line">    output = self.conv3(output)</span><br><span class="line">    output = self.conv4(output)</span><br><span class="line">    output = self.conv5(output)</span><br><span class="line">    output = self.avg_pool(output)</span><br><span class="line">    output = output.view(output.size(<span class="number">0</span>), -<span class="number">1</span>)</span><br><span class="line">    output = self.fc(output)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> output</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">resnet34</span>():</span></span><br><span class="line">  <span class="keyword">return</span> ResNet(BasicBlock, [<span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">3</span>])</span><br></pre></td></tr></table></figure>
<h2><span id="定义训练-验证和测试方法">定义训练、验证和测试方法</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">check_accuracy</span>(<span class="params">loader, model</span>):</span></span><br><span class="line">    <span class="keyword">if</span> loader.dataset.train:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;Checking accuracy on validation set&#x27;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;Checking accuracy on test set&#x27;</span>)   </span><br><span class="line">    num_correct = <span class="number">0</span></span><br><span class="line">    num_samples = <span class="number">0</span></span><br><span class="line">    model.<span class="built_in">eval</span>()  <span class="comment"># set model to evaluation mode</span></span><br><span class="line">    <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">        <span class="keyword">for</span> x, y <span class="keyword">in</span> loader:</span><br><span class="line">            x = x.to(device=device, dtype=dtype)  <span class="comment"># move to device, e.g. GPU</span></span><br><span class="line">            y = y.to(device=device, dtype=torch.long)</span><br><span class="line">            scores = model(x)</span><br><span class="line">            _, preds = scores.<span class="built_in">max</span>(<span class="number">1</span>)</span><br><span class="line">            num_correct += (preds == y).<span class="built_in">sum</span>()</span><br><span class="line">            num_samples += preds.size(<span class="number">0</span>)</span><br><span class="line">        acc = <span class="built_in">float</span>(num_correct) / num_samples</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;Got %d / %d correct (%.2f)&#x27;</span> % (num_correct, num_samples, <span class="number">100</span> * acc))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">train</span>(<span class="params">model, optimizer, epochs=<span class="number">1</span></span>):</span></span><br><span class="line">  loss_history = []</span><br><span class="line">  print_every = config[<span class="string">&#x27;print_every&#x27;</span>]</span><br><span class="line">  model = model.to(device=device)</span><br><span class="line">  <span class="keyword">for</span> e <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;Epoch %d&#x27;</span> % (e+<span class="number">1</span>))</span><br><span class="line">    <span class="keyword">for</span> t, (x, y) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">      model.train()</span><br><span class="line">      x = x.to(device=device, dtype=dtype)</span><br><span class="line">      y = y.to(device=device, dtype=torch.long)</span><br><span class="line"></span><br><span class="line">      scores = model(x)</span><br><span class="line">      loss = F.cross_entropy(scores, y)</span><br><span class="line"></span><br><span class="line">      optimizer.zero_grad()</span><br><span class="line"></span><br><span class="line">      loss.backward()</span><br><span class="line"></span><br><span class="line">      optimizer.step()</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> t % print_every == <span class="number">0</span>:</span><br><span class="line">        loss_history.append(loss.item())</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;Iteration %d, loss = %.4f&#x27;</span> % (t, loss.item()))</span><br><span class="line">        <span class="comment">#check_accuracy(val_loader, model)</span></span><br><span class="line">        <span class="comment">#print()</span></span><br><span class="line">  <span class="keyword">return</span> loss_history</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span>(<span class="params">model</span>):</span></span><br><span class="line">  check_accuracy(test_loader, model)</span><br><span class="line">  <span class="built_in">print</span>()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">model = <span class="literal">None</span></span><br><span class="line">model = resnet34()</span><br><span class="line">learning_rate = <span class="number">1e-2</span></span><br><span class="line">epochs = <span class="number">12</span></span><br><span class="line">optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=<span class="number">0.9</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">loss_history = train(model, optimizer, epochs) <span class="comment"># 最终训练集上的loss在0.05左右</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">plt.plot(loss_history)</span><br><span class="line">plt.xlabel(<span class="string">&#x27;iteration&#x27;</span>)</span><br><span class="line">plt.ylabel(<span class="string">&#x27;loss&#x27;</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/ResNet/1.png" alt></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test(model) <span class="comment"># Got 8355 / 10000 correct (83.55)</span></span><br></pre></td></tr></table></figure>
<h2><span id="保存模型参数">保存模型参数</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">torch.save(model.state_dict(), <span class="string">&#x27;ResNet34_params.pth&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2><span id="对输入的图片进行预测">对输入的图片进行预测</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">predict</span>(<span class="params">image</span>):</span></span><br><span class="line">  class_indict = &#123;</span><br><span class="line">      <span class="number">0</span>: <span class="string">&#x27;airplane&#x27;</span>,</span><br><span class="line">      <span class="number">1</span>: <span class="string">&#x27;automobile&#x27;</span>,</span><br><span class="line">      <span class="number">2</span>: <span class="string">&#x27;bird&#x27;</span>,</span><br><span class="line">      <span class="number">3</span>: <span class="string">&#x27;cat&#x27;</span>,</span><br><span class="line">      <span class="number">4</span>: <span class="string">&#x27;deer&#x27;</span>,</span><br><span class="line">      <span class="number">5</span>: <span class="string">&#x27;dog&#x27;</span>,</span><br><span class="line">      <span class="number">6</span>: <span class="string">&#x27;frog&#x27;</span>,</span><br><span class="line">      <span class="number">7</span>: <span class="string">&#x27;horse&#x27;</span>,</span><br><span class="line">      <span class="number">8</span>: <span class="string">&#x27;ship&#x27;</span>,</span><br><span class="line">      <span class="number">9</span>: <span class="string">&#x27;truck&#x27;</span>,</span><br><span class="line">  &#125;</span><br><span class="line">  transform = T.Compose([</span><br><span class="line">      T.Resize((<span class="number">32</span>, <span class="number">32</span>)), <span class="comment"># CIFAR10本身就是32x32的RGB图</span></span><br><span class="line">      T.ToTensor(), <span class="comment"># 转换为张量</span></span><br><span class="line">      T.Normalize((<span class="number">0.4914</span>, <span class="number">0.4822</span>, <span class="number">0.4465</span>), (<span class="number">0.2023</span>, <span class="number">0.1994</span>, <span class="number">0.2010</span>))</span><br><span class="line">  ])</span><br><span class="line"></span><br><span class="line">  <span class="comment"># load image</span></span><br><span class="line">  img_path = <span class="string">&#x27;test_imgs/&#123;&#125;.jpg&#x27;</span>.<span class="built_in">format</span>(image)</span><br><span class="line">  <span class="keyword">assert</span> os.path.exists(img_path), <span class="string">&quot;file: &#x27;&#123;&#125;&#x27; dose not exist.&quot;</span>.<span class="built_in">format</span>(img_path)</span><br><span class="line"></span><br><span class="line">  img = Image.<span class="built_in">open</span>(img_path)</span><br><span class="line">  plt.imshow(img)</span><br><span class="line">  img = transform(img)</span><br><span class="line">  img = torch.unsqueeze(img, dim=<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">  <span class="comment"># ceate model</span></span><br><span class="line">  model = resnet34()</span><br><span class="line"></span><br><span class="line">  <span class="comment"># load model weights</span></span><br><span class="line">  model_params_path = <span class="string">&#x27;ResNet34_params.pth&#x27;</span></span><br><span class="line">  model.load_state_dict(torch.load(model_params_path))</span><br><span class="line">  model.<span class="built_in">eval</span>()</span><br><span class="line"></span><br><span class="line">  <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">    <span class="comment"># predic class</span></span><br><span class="line">    output = torch.squeeze(model(img))</span><br><span class="line">    predict = torch.softmax(output, dim=<span class="number">0</span>)</span><br><span class="line">    predict_class = torch.argmax(predict).numpy()</span><br><span class="line"></span><br><span class="line">  the_class = class_indict[<span class="built_in">int</span>(predict_class)]</span><br><span class="line">  prob = predict[predict_class].numpy()</span><br><span class="line"></span><br><span class="line">  plt.title(<span class="string">&#x27;Class: &#123;&#125;;  Prob: &#123;:.3&#125;&#x27;</span>.<span class="built_in">format</span>(the_class, prob))</span><br><span class="line">  plt.show()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">image = <span class="string">&#x27;lu&#x27;</span></span><br><span class="line">predict(image)</span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/ResNet/2.png" alt></p>
<h2><span id="mobilenetv2部分仅包含模型">MobileNetV2部分（仅包含模型）</span></h2>
<p>该模型在学习率为0.01，训练10轮（其他配置和前面的代码一样）的情况下，最终在训练集上的loss为0.2+，在测试集上的准确率为76.33%；事实上，本着体验搭建模型流程的初衷，几乎没有怎么调参，所以可以认为，在参数调好之后，模型表现会更好。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinearBottleNeck</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  <span class="comment"># t: expansion factor</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, in_channels, out_channels, stride, t=<span class="number">6</span>, class_num=<span class="number">10</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    self.residual = nn.Sequential(</span><br><span class="line">      nn.Conv2d(in_channels=in_channels, out_channels=in_channels * t, kernel_size=<span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(in_channels * t),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>), <span class="comment"># inplace=True means to change the original inputs</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># groups=in_channels * t means to do depth-wise convolution</span></span><br><span class="line">      nn.Conv2d(in_channels * t, in_channels * t, <span class="number">3</span>, stride=stride, padding=<span class="number">1</span>, groups=in_channels * t),</span><br><span class="line">      nn.BatchNorm2d(in_channels * t),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line"></span><br><span class="line">      nn.Conv2d(in_channels * t, out_channels, <span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(out_channels),</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    self.stride = stride</span><br><span class="line">    self.in_channels = in_channels</span><br><span class="line">    self.out_channels = out_channels</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">    residual = self.residual(x)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> self.stride == <span class="number">1</span> <span class="keyword">and</span> self.in_channels == self.out_channels:</span><br><span class="line">      residual += x</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> residual</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MobileNetV2</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, class_num=<span class="number">10</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    self.pre = nn.Sequential(</span><br><span class="line">      nn.Conv2d(<span class="number">3</span>, <span class="number">32</span>, <span class="number">1</span>, padding=<span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(<span class="number">32</span>),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    self.layer1 = LinearBottleNeck(<span class="number">32</span>, <span class="number">16</span>, <span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">    self.layer2 = self._make_layer(<span class="number">2</span>, <span class="number">16</span>, <span class="number">24</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer3 = self._make_layer(<span class="number">3</span>, <span class="number">24</span>, <span class="number">32</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer4 = self._make_layer(<span class="number">4</span>, <span class="number">32</span>, <span class="number">64</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer5 = self._make_layer(<span class="number">3</span>, <span class="number">64</span>, <span class="number">96</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer6 = self._make_layer(<span class="number">3</span>, <span class="number">96</span>, <span class="number">160</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer7 = LinearBottleNeck(<span class="number">160</span>, <span class="number">320</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line">    self.conv1 = nn.Sequential(</span><br><span class="line">      nn.Conv2d(<span class="number">320</span>, <span class="number">1280</span>, <span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(<span class="number">1280</span>),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    self.conv2 = nn.Conv2d(<span class="number">1280</span>, class_num, <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">    x = self.pre(x)</span><br><span class="line">    x = self.layer1(x)</span><br><span class="line">    x = self.layer2(x)</span><br><span class="line">    x = self.layer3(x)</span><br><span class="line">    x = self.layer4(x)</span><br><span class="line">    x = self.layer5(x)</span><br><span class="line">    x = self.layer6(x)</span><br><span class="line">    x = self.layer7(x)</span><br><span class="line">    x = self.conv1(x)</span><br><span class="line">    x = F.adaptive_avg_pool2d(x, output_size=<span class="number">1</span>)</span><br><span class="line">    x = self.conv2(x)</span><br><span class="line">    x = x.view(x.size(<span class="number">0</span>), -<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> x</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">_make_layer</span>(<span class="params">self, repeat, in_channels, out_channels, stride, t</span>):</span></span><br><span class="line">    layers = []</span><br><span class="line">    layers.append(LinearBottleNeck(in_channels, out_channels, stride, t))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> repeat - <span class="number">1</span>:</span><br><span class="line">      layers.append(LinearBottleNeck(out_channels, out_channels, <span class="number">1</span>, t))</span><br><span class="line">      repeat -= <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> nn.Sequential(*layers)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mobilenetv2</span>():</span></span><br><span class="line">  <span class="keyword">return</span> MobileNetV2()</span><br></pre></td></tr></table></figure>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://tina-yao.gitee.io/bigbig-shark/2022/08/06/%E4%BD%BF%E7%94%A8ResNet34%E5%92%8CMobileNetV2%E5%AD%A6%E4%B9%A0CIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86/" title="使用ResNet34和MobileNetV2学习CIFAR10数据集" target="_blank" rel="external">http://tina-yao.gitee.io/bigbig-shark/2022/08/06/使用ResNet34和MobileNetV2学习CIFAR10数据集/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/bigbig-shark/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank"><span class="text-dark">大鲨鱼</span><small class="ml-1x">CV&amp;Robots</small></a></h3>
        <div>格物致知，诚意力行。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/bigbig-shark/2022/08/15/ObjectDetection-1-RCNN/" title="ObjectDetection(1)_RCNN"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;Newer</span></a>
    </li>
    
    
    <li class="next">
      <a href="/bigbig-shark/2022/07/27/%E8%AF%86%E5%88%AB%E5%B0%8F%E8%BD%A6%E8%BA%AB%E4%B8%8A%E7%9A%84%E7%89%B9%E5%AE%9A%E4%BA%AE%E6%9D%A1/" title="识别小车身上的特定亮条"><span>Older&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>$</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat" data-mobile-sites="weibo,qq"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>Maybe you could buy me a cup of coffee.</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/alipayimg.PNG#images/donate/alipayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open alipay app scan this qrcode, buy me a coffee!</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/wechatpayimg.PNG#images/donate/wechatpayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open wechat app scan this qrcode, buy me a coffee!</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> alipay</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> wechat payment</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        &copy; 2023 BigbigShark
        
        <div class="publishby">
        <!--
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        -->
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/bigbig-shark/js/plugin.min.js"></script>


<script src="/bigbig-shark/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/bigbig-shark/',
        CONTENT_URL: '/bigbig-shark/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/bigbig-shark/js/insight.js"></script>






   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '8pTCCvyyLGXskH0XW0fWHg7l-gzGzoHsz',
    appKey: 'id3rlCPRdbEoSpUfhJgcLhDO',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: true
  });
  </script>

     







<script src="/bigbig-shark/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/bigbig-shark/live2dw/assets/tororo.model.json"},"display":{"position":null,"width":300,"height":600},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>